package android.slc.appbase.ui.vd;

import android.view.View;

import androidx.activity.result.ActivityResultCaller;
import androidx.annotation.NonNull;
import androidx.databinding.Observable;
import androidx.lifecycle.LifecycleOwner;

import com.blankj.utilcode.util.ObjectUtils;

/**
 * 基础的详细盒子vm
 *
 * @author slc
 * @date 2020-08-25 16:13
 */
public class BaseDataChangeVd<VB extends BaseDataChangeVmBox> extends BaseAppVd<VB> {
    protected BaseDataChangeVmBox linkageDataChangeVmBox;

    private final Observable.OnPropertyChangedCallback vmBoxNotifyRefreshByGlobalCallback = new Observable.OnPropertyChangedCallback() {
        @Override
        public void onPropertyChanged(Observable sender, int propertyId) {
            notifyPageShelfRefresh(vmBox.notifyRefreshByGlobal.get());
        }
    };

    private final Observable.OnPropertyChangedCallback linkageDataChangeNotifyRefreshByGlobalCallback = new Observable.OnPropertyChangedCallback() {
        @Override
        public void onPropertyChanged(Observable sender, int propertyId) {
            notifyVmRefresh(linkageDataChangeVmBox.notifyRefreshByGlobal.get());
        }
    };

    public void init(ActivityResultCaller activityResultCaller, View contentView, VB vmBox, BaseDataChangeVmBox linkageDataChangeVmBox) {
        this.linkageDataChangeVmBox = linkageDataChangeVmBox;
        super.init(activityResultCaller, contentView, vmBox);
    }

    protected void initView() {

    }

    protected void initMustCondition() {
        if (vmBox != null) {
            vmBox.notifyRefreshByGlobal.addOnPropertyChangedCallback(vmBoxNotifyRefreshByGlobalCallback);
        }
        if (linkageDataChangeVmBox != null) {
            linkageDataChangeVmBox.notifyRefreshByGlobal.addOnPropertyChangedCallback(linkageDataChangeNotifyRefreshByGlobalCallback);
        }
    }

    protected void notifyVmRefresh(Object data) {
        if (vmBox != null) {
            vmBox.refresh();
        }
    }

    protected void notifyPageShelfRefresh(Object data) {
        if (linkageDataChangeVmBox != null) {
            if (ObjectUtils.equals(linkageDataChangeVmBox.notifyRefreshByGlobal.get(), data)) {
                linkageDataChangeVmBox.notifyRefreshByGlobal.notifyChange();
            } else {
                linkageDataChangeVmBox.notifyRefreshByGlobal.set(data);
            }
        } else {
            notifyVmRefresh(data);
        }
    }

    public <T extends BaseCommonPageShelfVmBox> T getLinkageDataChangeVmBox() {
        if (linkageDataChangeVmBox != null) {
            return (T) linkageDataChangeVmBox;
        }
        return null;
    }

    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        super.onDestroy(owner);
        if (vmBox != null) {
            vmBox.notifyRefreshByGlobal.removeOnPropertyChangedCallback(vmBoxNotifyRefreshByGlobalCallback);
        }
        if (linkageDataChangeVmBox != null) {
            linkageDataChangeVmBox.notifyRefreshByGlobal.removeOnPropertyChangedCallback(linkageDataChangeNotifyRefreshByGlobalCallback);
        }
    }
}
